package com.tgy.leetcode;

/**
 * https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/
 */
public class _81_搜索旋转排序数组_II {

    public static boolean  search(int[] nums, int target) {

        int index = 0;
        for (int i = 1; i < nums.length; i++) {

            if (nums[i] < nums[i - 1]) {

                index = i;
                break;
            }
        }


        int left,right, mid;
        int len = nums.length;
        if (nums[len - 1] == target) {

            return true;
        }else if (nums[len - 1] < target) {

            left = 0;
            right = index - 1;
        }else {

            left = index;
            right = len - 1;
        }

        while (left <= right) {

            mid = left + ((right - left) >> 1);

            if (nums[mid] == target) {

                return true;
            }else if (nums[mid] > target) {

                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }

        return false;
    }

    public static void main(String[] args) {

        int[] nums = {2,5,6,0,0,1,2};

        System.out.println(search(nums, 2));

    }
}
